<?php
class CommentsController extends AppController {

	var $name = 'Comments';

	function index() {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			$this->Comment->recursive = 0;
			$this->set('comments', $this->paginate());
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function view($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id) {
				$this->Session->setFlash(__('Invalid comment', true));
				$this->redirect(array('action' => 'index'));
			}
			$this->set('comment', $this->Comment->read(null, $id));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function add() {
		if(empty($this->data)){
			
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
		if (!empty($this->data) && $this->Auth->user('id')) {
			$data = $this->data;
			$isCommented = $this->Comment->find('count',array('conditions'=>array('product_id'=>$data['Comment']['product_id'], 'user_id'=>$data['Comment']['user_id'])));
			if($isCommented == 0){
				$this->Comment->create();
				if ($this->Comment->save($this->data)) {
					$id = $this->Comment->id;
	                $this->Comment->recursive = -1;
	                if($this->RequestHandler->isAjax()){
		                $this->autoRender = false;
		                $comment = $this->Comment->findById($id);
		                App::import('Helper', 'Time');
		                $time = new TimeHelper();
		                $comment['Comment']['created'] = $time->format($format = 'd.m.Y H:i', $comment['Comment']['created']);
		                $comment['Comment']['modified'] = $time->format($format = 'd.m.Y H:i', $comment['Comment']['modified']);
		                return(json_encode($comment));
					}else {
						$this->Session->setFlash(__('The comment has been saved', true));
	                	$this->redirect(array('action' => 'index'));
					}
				} else {
					$this->Session->setFlash(__('The comment could not be saved. Please, try again.', true));
				}
			}
		}
		$products = $this->Comment->Product->find('list');
		$users = $this->Comment->User->find('list');
		$this->set(compact('products', 'users'));
	}

	function edit($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id && empty($this->data)) {
				$this->Session->setFlash(__('Invalid comment', true));
				$this->redirect(array('action' => 'index'));
			}
			if (!empty($this->data)) {
				if ($this->Comment->save($this->data)) {
					$this->Session->setFlash(__('The comment has been saved', true));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The comment could not be saved. Please, try again.', true));
				}
			}
			if (empty($this->data)) {
				$this->data = $this->Comment->read(null, $id);
			}
			$products = $this->Comment->Product->find('list');
			$users = $this->Comment->User->find('list');
			$this->set(compact('products', 'users'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}

	function delete($id = null) {
		$user = $this->Session->read('Auth.User');
		if($user['user_role']=='admin'){
			$this->layout = 'admin';
			if (!$id) {
				$this->Session->setFlash(__('Invalid id for comment', true));
				$this->redirect(array('action'=>'index'));
			}
			if ($this->Comment->delete($id)) {
				$this->Session->setFlash(__('Comment deleted', true));
				$this->redirect(array('action'=>'index'));
			}
			$this->Session->setFlash(__('Comment was not deleted', true));
			$this->redirect(array('action' => 'index'));
		} else {
			$this->Session->setFlash(__('Bu sayfaya erişmeye yetkiniz yoktur!', true));
			$this->redirect(array('controller'=>'pages','action' => 'index'));
		}
	}
	
	function beforeFilter() {
	    parent::beforeFilter();
	    $this->Auth->allow(array('*'));
    }
}
